import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/utils/helper.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';

///选择时间
class SelectTime extends StatefulWidget {
  ///
  final ValueChanged<String> onCheckUpdate;

  ///
  const SelectTime({Key key, this.onCheckUpdate}) : super(key: key);

  @override
  _SelectTimeState createState() => _SelectTimeState();
}

class _SelectTimeState extends State<SelectTime> {
  String _start = "";
  String _end = "";
  List<String> time = [
    "00",
    "01",
    "02",
    "03",
    "04",
    "05",
    "06",
    "07",
    "08",
    "09",
    "10",
    "11",
    "12",
    "13",
    "14",
    "15",
    "16",
    "17",
    "18",
    "19",
    "20",
    "21",
    "22",
    "23",
    "24",
  ];
  FixedExtentScrollController _startController =
      FixedExtentScrollController(initialItem: 0);
  FixedExtentScrollController _endController =
      FixedExtentScrollController(initialItem: 0);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: setWidth(345),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            margin: EdgeInsets.only(top: setWidth(40)),
            height: setWidth(176),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Expanded(
                  child: CupertinoPicker(
                    scrollController: _startController,
                    useMagnifier: true,
                    magnification: 1.1,
                    itemExtent: 44,
                    onSelectedItemChanged: (index) {
                      // if (int.parse(time[index]) >=
                      //     int.parse(time[_endController.selectedItem])) {
                      //   _endController.jumpToItem(index);
                      // }
                      _start = time[index];
                    },
                    children: time
                        .asMap()
                        .map((key, value) {
                          return MapEntry(
                              key,
                              Container(
                                alignment: Alignment.center,
                                child: Text(
                                  "$value:00",
                                  style: TextStyle(
                                      color: Color(0xff333333),
                                      fontSize: setSp(20)),
                                ),
                              ));
                        })
                        .values
                        .toList(),
                  ),
                ),
                Expanded(
                  child: CupertinoPicker(
                    scrollController: _endController,
                    useMagnifier: true,
                    magnification: 1.1,
                    itemExtent: 44,
                    onSelectedItemChanged: (index) {
                      // if (int.parse(time[index]) <=
                      //     int.parse(time[_startController.selectedItem])) {
                      //   _startController.jumpToItem(index);
                      // }
                      _end = time[index];
                    },
                    children: time
                        .asMap()
                        .map((key, value) {
                          return MapEntry(
                              key,
                              Container(
                                alignment: Alignment.center,
                                child: Text(
                                  "$value:00",
                                  style: TextStyle(
                                      color: Color(0xff333333),
                                      fontSize: setSp(20)),
                                ),
                              ));
                        })
                        .values
                        .toList(),
                  ),
                )
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(bottom: ScreenUtil.bottomBarHeight + 35),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                GestureDetector(
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: Container(
                    padding: EdgeInsets.only(
                        left: setWidth(42),
                        top: setWidth(12),
                        bottom: setWidth(12),
                        right: setWidth(42)),
                    decoration: BoxDecoration(
                      color: Color(0xffF2F2F2),
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: Text(
                      "取消",
                      style: TextStyle(
                          color: Color(0xff010015), fontSize: setSp(15)),
                    ),
                  ),
                ),
                SizedBox(
                  width: setWidth(30),
                ),
                GestureDetector(
                  onTap: () {
                    if (int.parse(time[_startController.selectedItem]) <=
                        int.parse(time[_endController.selectedItem])) {
                      widget.onCheckUpdate("$_start:00 - $_end:00");
                      Navigator.pop(context);
                    } else {
                      toast("结束时间必须大于开始时间");
                    }
                  },
                  child: Container(
                    padding: EdgeInsets.only(
                        left: setWidth(42),
                        top: setWidth(12),
                        bottom: setWidth(12),
                        right: setWidth(42)),
                    decoration: BoxDecoration(
                      color: YTTheme.theme,
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: Text(
                      "确定",
                      style:
                          TextStyle(color: Colors.white, fontSize: setSp(15)),
                    ),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
